/*
 * @Author       : wfl
 * @LastEditors: qiye
 * @description  :
 * @updateInfo   :
 * @Date         : 2023-03-02 09:54:42
 * @LastEditTime: 2024-02-05 18:13:24
 */
import type { PluginOption } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import vueLegacy from '@vitejs/plugin-legacy'
import { createHtmlPlugin } from 'vite-plugin-html'
import visualizer from 'rollup-plugin-visualizer'
// import vueTsc from 'vue-tsc'

// import createInspector from './inspector'
import createAutoImport from './auto-import'
import createComponents from './components'
import createSetupExtend from './setup-extend'
import createUnocss from './unocss'
import createSvgIcon from './svg-icon'
import createI18n from './i18n'
import createLayouts from './layouts'
import createPages from './pages'
import createCompression from './compression'
import createBanner from './banner'
import createLog from './log'
import createFolderTree from './create-folder-tree'

export default function createVitePlugins(viteEnv: any, isBuild = false) {
  const vitePlugins: (PluginOption | PluginOption[] | any)[] = [
    vue({
      script: {
        defineModel: true
      }
    }),
    vueJsx(),
    // vueTsc({
    //   diagnosticLogs: true
    // }),
    vueLegacy({
      renderLegacyChunks: false,
      modernPolyfills: [
        'es.array.at'
      ]
    }),
    createFolderTree(viteEnv),
    createHtmlPlugin({
      // viteNext: true,
      minify: true,
      entry: '/src/MicroApp/micro.ts',
      inject: {
        data: {
          title: viteEnv.VITE_APP_TITLE,
          icon: viteEnv.VITE_APP_ICON,
          downloadUrl: viteEnv.VITE_APP_DOWNLOAD_URL
        }
      }
    })
  ]
  // vitePlugins.push(createInspector())
  vitePlugins.push(createAutoImport())
  vitePlugins.push(createComponents())
  vitePlugins.push(createSetupExtend())
  vitePlugins.push(createUnocss())
  vitePlugins.push(createSvgIcon(isBuild))
  vitePlugins.push(createI18n())
  vitePlugins.push(createLayouts())
  vitePlugins.push(createPages())
  isBuild && vitePlugins.push(...createCompression(viteEnv))
  // 自动生成 精灵图
  // vitePlugins.push(...createSpritesmith(isBuild))
  vitePlugins.push(createBanner())
  vitePlugins.push(createLog())
  vitePlugins.push(visualizer({
    open: true
  }))
  return vitePlugins
}
